package com.top.mall.main.income

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ConvertUtils
import com.google.android.material.appbar.AppBarLayout
import com.module.lemlin.owner.OwnerViewModeActivity
import com.top.mall.R
import com.top.mall.jetpack.vm.CouponViewModel
import kotlinx.android.synthetic.main.activity_coupon_center.*
import kotlinx.android.synthetic.main.activity_coupon_center.imageViewBack
import kotlinx.android.synthetic.main.item_tab_time.view.*

class CouponCenterActivity : OwnerViewModeActivity<CouponViewModel>(),
    AppBarLayout.OnOffsetChangedListener {

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, CouponCenterActivity::class.java)
            activity.startActivity(intent)
        }
    }

    override fun stateBar(): StatusBar {
        return StatusBar(view = clIncomeBar)
    }

    override fun viewModel(): CouponViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_coupon_center

    override fun initView() {

        imageViewBack.setOnClickListener { finish() }
        //设置状态栏颜色
        BarUtils.setStatusBarColor(this, Color.WHITE)
        appBarCoupon.addOnOffsetChangedListener(this)

        tlCouponCenterTime.apply {
            val tabs: MutableList<String> = mutableListOf<String>(
                "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00"
            )
            for ((index: Int, item: String) in tabs.withIndex()) addTab(newTab().apply {
                val view: View = LayoutInflater.from(this@CouponCenterActivity)
                    .inflate(R.layout.item_tab_time, null)
                view.tvTabGoodName.text = item
                view.tvTabGoodSub.setTextColor(
                    ContextCompat.getColorStateList(
                        this@CouponCenterActivity, R.color.tv_selected_white_orange
                    )
                )
                when (index) {
                    0 -> {
                        view.setPadding(
                            ConvertUtils.dp2px(15f), 0,
                            ConvertUtils.dp2px(6.5f), 0
                        )
                    }
                    tabs.size - 1 -> {
                        view.setPadding(
                            ConvertUtils.dp2px(6.5f), 0,
                            ConvertUtils.dp2px(15f), 0
                        )
                    }
                    else -> {
                        view.setPadding(
                            ConvertUtils.dp2px(6.5f), 0,
                            ConvertUtils.dp2px(6.5f), 0
                        )
                    }
                }
                customView = view
                tag = index
            })
        }

        vpCouponCenterBody.adapter = object : FragmentStatePagerAdapter(
            supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
        ) {
            val tabs: MutableList<String> = mutableListOf<String>(
                "全部", "品类券", "疯狂周末", "区域专享"
            )

            override fun getItem(position: Int): Fragment =
                CouponFragment.newInstance()

            override fun getCount(): Int = tabs.size

            override fun getPageTitle(position: Int): CharSequence? {
                return tabs[position]
            }

            override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {}
        }

        tlCouponCenterType.setupWithViewPager(vpCouponCenterBody)
    }

    override fun initData() {

    }

    override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
        if (verticalOffset * -1 == appBarCoupon.totalScrollRange) {
            tlCouponCenterType.setBackgroundResource(R.color.colorWhite)
        } else {
            tlCouponCenterType.setBackgroundResource(R.color.transparent)
        }
    }


}